package cz.cvut.fit.sin.eventlocator.core;

import java.io.InputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.springframework.web.util.UriUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import cz.cvut.fit.sin.eventlocator.data.Event;
import cz.cvut.fit.sin.eventlocator.data.Location;

/**
 * Downloads events from EventFul
 * 
 */
public class EventfulGrabber {

	private static final String BASE_URL = "http://api.eventful.com/rest/events/search?app_key=SNdpTG6w3JtfcF46&units=km&within=1&page_size=40&location=";
	private static final SimpleDateFormat DATE_FORMAT_FROM = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	private static final SimpleDateFormat DATE_FORMAT_TO = new SimpleDateFormat("MMMMMMMM dd, yyyy hh:mm");

	public List<Event> getEvents(Location location) throws Exception {
		String url = UriUtils.encodeHttpUrl(BASE_URL + location.getLatitude() + "," + location.getLongitude(), "UTF-8");
		return parseEvents(new URL(url).openStream());
	}

	private List<Event> parseEvents(InputStream is) throws Exception {
		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		Document document = builder.parse(is);

		List<Event> returnList = new ArrayList<Event>();
		NodeList events = document.getElementsByTagName("event");
		for (int i = 0; i < events.getLength(); i++) {
			returnList.add(createEvent(events.item(i)));
		}
		return returnList;
	}

	private Event createEvent(Node node) throws Exception {
		Event event = new Event();
		NodeList children = node.getChildNodes();
		for (int i = 0; i < children.getLength(); i++) {
			Node child;
			if ((child = children.item(i)).getNodeName().equals("title")) {
				event.setTitle(child.getTextContent().trim());
			} else if ((child = children.item(i)).getNodeName().equals("url")) {
				event.setUrl(child.getTextContent());
			} else if ((child = children.item(i)).getNodeName().equals("description")) {
				event.setDescription(child.getTextContent().trim());
			} else if ((child = children.item(i)).getNodeName().equals("start_time")) {
				event.setStartTime(DATE_FORMAT_TO.format(DATE_FORMAT_FROM.parse(child.getTextContent())));
			} else if ((child = children.item(i)).getNodeName().equals("latitude")) {
				event.setLatitude(Double.parseDouble(child.getTextContent()));
			} else if ((child = children.item(i)).getNodeName().equals("longitude")) {
				event.setLongitude(Double.parseDouble(child.getTextContent()));
			} else if ((child = children.item(i)).getNodeName().equals("image")) {
				NodeList childrenChildren = child.getChildNodes();
				for (int j = 0; j < childrenChildren.getLength(); j++) {
					Node childChild;
					if ((childChild = childrenChildren.item(j)).getNodeName().equals("url")) {
						event.setImageUrl(childChild.getTextContent());
					}
				}
			}
		}
		return event;
	}

}
